//
//  DCSettingViewController.m
//  Demo
//
//  Created by Kyle on 2023/1/1.
//

#import "DCSettingViewController.h"
#import "DCTSettingVCbleCell.h"
#import "DCLoginViewController.h"
#import "DCFeedBookViewController.h"

@interface DCSettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation DCSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitleName:@"Settings"];
}

- (void)configureAlloc {
    [super configureAlloc];
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *applocalversion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    self.dataSource = @[@{@"titleName":@"Feedback",@"rightName":@""},
                        @{@"titleName":@"Version number",@"rightName":[NSString stringWithFormat:@"Version %@",applocalversion]}].mutableCopy;
}
- (void)configureUI {
    [super configureUI];

    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.tableView registerClass:DCTSettingVCbleCell.class forCellReuseIdentifier:NSStringFromClass(DCTSettingVCbleCell.class)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = self.view.backgroundColor ;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.navgitBar.mas_bottom);
        make.left.right.bottom.mas_equalTo(0);
    }];
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 100)];
    UIButton *logoutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    logoutBtn.backgroundColor = [UIColor colorWithString:@"#1C221B"];
    [logoutBtn setTitle:@"Log out" forState:UIControlStateNormal];
    logoutBtn.layer.cornerRadius = 8;
    logoutBtn.layer.masksToBounds = YES;
    logoutBtn.titleLabel.font = DCFont_PF_Semibold(15);
    [logoutBtn setTitleColor:[UIColor colorWithString:@"#FFFFFF"] forState:UIControlStateNormal];
    [logoutBtn addTarget:self action:@selector(logoutAction:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:logoutBtn];
    [logoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(38);
        make.right.mas_equalTo(-38);
        make.height.mas_equalTo(50);
        make.centerY.mas_equalTo(footerView);
    }];
    self.tableView.tableFooterView = footerView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DCTSettingVCbleCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(DCTSettingVCbleCell.class)];
    NSDictionary *dic = [self.dataSource objectAtIndex:indexPath.row];
    NSString *titleName = [dic objectForKey:@"titleName"];
    NSString *rightName = [dic objectForKey:@"rightName"];
    cell.leftLabel.text = titleName;
    cell.rightLabel.text = rightName;
    cell.rightLabel.hidden = indexPath.row ==0;
    cell.bottomLine.hidden = indexPath.row == 1;
    cell.rightImage.hidden = indexPath.row == 1;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 68;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        DCFeedBookViewController *vc = [DCFeedBookViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        
    }
}

- (void)logoutAction:(UIButton *)sender {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [DCHttpRequest DCLogoutCompleteBlock:^(NSURLSessionDataTask * _Nonnull operation, DCResponseData * _Nonnull responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (responseObject.code == 200) {
            [[CacheTools sharedCacheTools] removekey:SMARTUSERINFO];
            DCLoginViewController *loginVC = [[DCLoginViewController alloc] init];
            UIWindow *keyWindow = [APPDelegate window];
            keyWindow.rootViewController = loginVC;
            [keyWindow makeKeyAndVisible];
        } else {
            [DCDCToast toastText:responseObject.message];
        }
    }];
}
@end
